<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 样板验收记录 -->
    <div id="base-breadcrumb" style="position: relative; overflow: hidden">
      <div id="print-box" style="position: absolute; z-index: -1"></div>
      <!-- 搜索 -->
      <searchData :searchData="searchArr" @search="search"></searchData>
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- <title-name :title="$route.meta.title"></title-name> -->
        <!-- 统计汇总 -->
        <!-- <AuditSummary title="需验收样板工程" :form="queryParam" :loadCount="loadCount" :api="api" :countList="countList" /> -->
        <!-- 表格 -->
        <list-table class="acceptanceRecord-table" ref="table" tableLayout="fixed" size="default" :columns="columns"  @select-click="selectClick" :request="loadData" :isShowCountBar="false" :selectConfig="{ use: true, type: 'checkbox', rowKey: 'id' }">
          <div class="mb12" slot="prefix" slot-scope="ref">
            <a-button v-btnPermission="'qm_013_add_01'" icon="plus" type="primary" @click="handleManage(2)">添加验收记录</a-button>
            <a-button v-btnPermission="'qm_013_sexp_05'" :loading="exportLoading[0]" icon="download" class="ml12" :disabled="!ref.selectedRowKeys.length" type="default" @click="exportWord(0, ref.selectedRowKeys)">{{
              rate[0] ? '导出中' + rate[0] : '选中导出'
            }}</a-button>
            <a-button v-btnPermission="'qm_013_aexp_06'" :loading="exportLoading[1]" icon="cloud-download" class="ml12" type="default" @click="exportWord(1)">{{ rate[1] ? '导出中' + rate[1] : '全部导出' }}</a-button>
          </div>
          <a-alert slot="bottom" class="mb12" type="info" show-icon>
            <template slot="message">
              {{ $route.meta.title }}
              <!-- 遍历汇总集合 -->
              <span>已选中:<a><b style="margin-left:5px">{{ selectLength || 0}}</b></a></span>
              <span style="margin-left:10px"
                >总数：
                <a
                  ><b>{{ total || 0 }}</b></a
                >个</span
              >
            </template>
          </a-alert>
          <template slot="action" slot-scope="text, record">
            <a v-btnPermission="'qm_013_loo_04'" @click="handleManage(0, record)">查看</a>
            <template v-if="record.statusCode === '0' || record.statusCode === '3'">
              <a-divider v-btnPermission="'qm_013_edi_02'" type="vertical"></a-divider>
              <a v-btnPermission="'qm_013_edi_02'" @click="handleManage(1, record)">编辑</a>
            </template>
          </template>
          <a-list slot="expandedRowRender" slot-scope="record" item-layout="horizontal" :data-source="formatItem(record)">
            <a-list-item slot="renderItem" slot-scope="item">
              <div>{{ item }}</div>
            </a-list-item>
          </a-list>
        </list-table>
      </a-card>
    </div>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import { acceptanceRecord } from '@/api/quality/sample' // api
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import OrgTreeSelect from '@/components/OrgTreeSelect'
import BasePage from '@/views/basePage'
// import AuditSummary from '@/views/quality/components/AuditSummary' // 统计汇总
// import SelectTeam from '@/components/Select/SelectTeam' // 班组
// import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import SelectProjectStatus from '@/views/quality/components/SelectProjectStatus' // 项目状态

const searchArr = [
  // 搜索框
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '分公司',
  //   type: SelectSubCompany,
  //   valueKey: 'branchCompanyId'
  // },
  // {
  //   name: '项目名称',
  //   type: SelectProject,
  //   valueKey: 'projectId'
  // },
  {
    name: '项目状态',
    type: SelectProjectStatus,
    valueKey: 'projectStatus'
  },
  // {
  //   name: '分包商',
  //   type: SelectSubcontractor,
  //   valueKey: 'subContractorId',
  // },
  // {
  //   name: '班组',
  //   type: SelectTeam,
  //   valueKey: 'monitorId',
  // },
  {
    name: '施工时间',
    type: 'dateRange',
    value: 'time',
    attr: {
      valueFormat: 'YYYY-MM-DD'
    }
  },
  {
    name: '样板名称',
    type: 'input',
    value: 'name'
  },
  {
    name: '样板部位',
    type: 'input',
    value: 'part'
  },
  {
    name: '劳务单位',
    type: 'input',
    value: 'subcontractor'
  }
]
const columns = [
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchCompanyName'
  },
  {
    title: '项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '样板名称',
    ellipsis: true,
    dataIndex: 'name'
  },
  {
    title: '样板部位',
    ellipsis: true,
    dataIndex: 'part'
  },
  {
    title: '施工时间',
    ellipsis: true,
    dataIndex: 'constructionDate'
  },
  {
    title: '劳务单位',
    ellipsis: true,
    dataIndex: 'subcontractor'
  },
  {
    title: '样板状态',
    dataIndex: 'sampleStatus',
    customRender(e) {
      return ['计划内', '计划外'][e] || ''
    }
  },
  {
    title: '状态',
    dataIndex: 'statusName'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    // align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable
    // AuditSummary,
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      rate: ['', ''], // 导出进度
      queryParam: {}, // 查询参数
      total: 0, // 总数
      selectLength:0,
      exportLoading: {
        0: false,
        1: false
      }, // 导出loading
      loadData: (parameter) => {
        let queryParam = this.queryParam
        if (queryParam.time) {
          queryParam.beginDate = queryParam.time[0]
          queryParam.endDate = queryParam.time[1]
          delete queryParam.time
        }
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return acceptanceRecord.postList(requestParameters).then((res) => {
          if (res.code === 200) {
            this.total = res.totalSize // 映射总数
            return res
          }
        })
      }, // 加载数据方法 必须为 Promise 对象
      editModel: 0,
      api: acceptanceRecord, // 汇总api
      loadCount: 0, // 汇总加载控件
      // countList:[
      //   {
      //     title: '总数：',
      //     value:'total'
      //   },
      //   {
      //     title: '已验收：',
      //     value:'accepted'
      //   },
      //   {
      //     title: '未验收：',
      //     value:'not'
      //   },
      //   {
      //     title: '验收率：',
      //     value:'acceptedPassRate',
      //     needUnit:false
      //   }
      // ], // 汇总列表
      form: {}
    }
  },
  methods: {
    selectClick(a,b){
      this.selectLength = a.rows.length;
    },
    /**
     * @description: 导出word
     * @param {*} index：导出类型：0 选中，1 全部
     * @param {*} datas：选中项
     */
    exportWord(index, datas) {
      let args = {}
      if (datas?.length) {
        args.ids = datas
      } else {
        args = this.queryParam
      }
      this.exportLoading[index] = true
      acceptanceRecord
        .batchPrint(args)
        .then((res) => {
          this.exportLoading[index] = false
          if (res.code === 200) {
            // 格式化名称，防止重名被覆盖
            res.data.forEach((i, idx) => {
              i.name = Number(idx) + 1 + '、' + i.name
              i.contentHtml = i.content
              delete i.content
            })
            this.exportPdf(
              res.data,
              '样板验收记录',
              (list) => {
                // console.log(list)
                this.rate[index] = (((list.length * 100) / res.data.length).toFixed(2) || 0) + '%'
              },
              () => {
                this.exportLoading[index] = false
                this.rate[index] = ''
              }
            )
          }
        })
        .catch(() => {
          this.exportLoading[index] = false
          this.rate[index] = ''
        })
    },
    // 格式化
    formatItem(e) {
      let datas = []
      let title = { content: '验收记录：', result: '验收结论：' }
      for (let index in e) {
        let item = e[index]
        for (let i in title) {
          if (i == index) {
            datas.push(title[i] + (e[index] || '暂无'))
          }
        }
      }
      return datas
    },
    // 搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      this.editModel = type
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: '/quality/modelProject/acceptanceRecord/acceptanceRecordEdit',
        query
      })
    }
  }
}
</script>
<style lang="less">
.acceptanceRecord-table .ant-table-fixed-right .ant-table-expanded-row {
    height: 110px;
}
</style>

